package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.vo.GoodsVO;
import com.dongdongshop.domin.vo.LoginUser;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
@CrossOrigin  //跨域
@RefreshScope   //自动监听配置文件
@Slf4j
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    //增加商品
    @PostMapping("addGoods")
    public Result addGoods(@RequestBody GoodsVO goodsVo, @RequestHeader String AutharizeToken) {
        //解析前端的token
        Claims claims = JwtUtil.parseJwt(AutharizeToken);
        //解析出商家数据
        LoginUser loginUser = JSONObject.parseObject(claims.getSubject(), LoginUser.class);
        //取出商家id
        goodsVo.setSellerId(loginUser.getId());
        Result result = goodsService.addGoods(goodsVo);
        return result;
    }

    @GetMapping("getGoodsById")
    public Result getGoodsById(@RequestParam Long goodsId){
        GoodsVO vo = goodsService.getGoodsById(goodsId);
        return Result.OK().setData(vo);
    }
}
